<script setup lang="ts">
import type { GuessPlayer } from '@/types/home'
import { getRecommendPlayersAPI } from '@/services/home'
import { ref, computed, onMounted } from 'vue'
import type { PageParams } from '@/types/global'

// 推荐玩家数据
const recommendList = ref<GuessPlayer[]>([])

// 分页参数
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}

// 猜你喜欢的列表
const guessList = ref<GuessPlayer[]>([])
// 已结束标记
const finish = ref(false)
// 获取猜你喜欢数据
const getRecommendPlayers = async () => {
  // 退出分页判断
  if (finish.value === true) {
    return uni.showToast({ icon: 'none', title: '没有更多数据~' })
  }
  const res = await getRecommendPlayersAPI(pageParams)
  // 数组追加
  guessList.value.push(...res.result.items)
  // 分页条件
  if (pageParams.page < res.result.pages) {
    // 页码累加
    pageParams.page++
  } else {
    finish.value = true
  }

  // 同步更新recommendList，确保模板可以正确显示
  recommendList.value = guessList.value
}

// 重置数据
const resetData = () => {
  pageParams.page = 1
  guessList.value = []
  recommendList.value = []
  finish.value = false
}

// 点击找TA陪玩
const orderPlayer = (player: GuessPlayer) => {
  // 将选中的玩家信息存储到本地
  uni.setStorageSync('selectedPlayer', player)
  // 跳转到下单页面（购物车页面）
  // 错误信息navigateTo:fail can not navigateTo a tabbar page表明您正在尝试使用navigateTo跳转到一个被定义为tabBar页面的页面。在uni-app中，这是不允许的。对于在tabBar中定义的页面，必须使用uni.switchTab进行跳转。
  // uni.navigateTo({
  uni.switchTab({
    url: '/pages/cart/cart',
  })
}

// 页面加载时获取数据
onMounted(() => {
  getRecommendPlayers()
})

// 暴露方法
defineExpose({
  resetData,
  getMore: getRecommendPlayers,
})
</script>

<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="section-title">猜你喜欢</text>
    <text class="section-subtitle">为你精选优质陪玩</text>
  </view>
  <!-- 推荐玩家列表 -->
  <view class="recommend-section">
    <!-- 玩家卡片列表 -->
    <view class="player-list">
      <view v-for="player in recommendList" :key="player.id" class="player-card">
        <!-- 玩家头像 -->
        <image class="player-avatar" :src="player.avatar" mode="aspectFill" />

        <!-- 玩家信息 -->
        <view class="player-info">
          <view class="player-header">
            <text class="player-nickname">{{ player.nickname }}</text>
            <view class="player-rating">
              <!-- 使用uni-icons组件并指定颜色 -->
              <uni-icons
                type="star-filled"
                class="rating-star"
                color="#59b6f7"
                size="16"
              ></uni-icons>
              <text class="rating-score">{{ player.rating }}</text>
            </view>
          </view>

          <text class="player-intro">{{ player.introduction }}</text>

          <view class="player-stats">
            <view class="stat-item">
              <text class="stat-label">近期服务</text>
              <text class="stat-value">{{ player.serviceCount }}人</text>
            </view>
          </view>

          <!-- 擅长游戏标签 -->
          <view class="player-tags">
            <view v-for="(tag, index) in player.tags" :key="index" class="tag-item">
              {{ tag }}
            </view>
          </view>

          <!-- 价格和按钮 -->
          <view class="player-footer">
            <view class="price-wrap">
              <text class="price-symbol">¥</text>
              <text class="price-value">{{ player.price }}</text>
              <text class="price-unit">/小时</text>
            </view>
            <!-- <button class="order-btn" @tap="orderPlayer(player)">找ta陪玩</button> -->
            <view class="order-btn" @tap="orderPlayer(player)">找ta陪玩</view>
          </view>
        </view>
      </view>
    </view>
  </view>
  <view class="loading-text">
    {{ finish ? '没有更多数据~' : '正在加载...' }}
  </view>
</template>

<style lang="scss" scoped>
:host {
  display: block;
}
/* 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  // padding: 36rpx 0 40rpx; 这个写法是 CSS 缩写形式，它按照以下顺序来设置内边距：
  // padding: <上> <右> <下> <左>;
  // 上：36rpx
  // 右：0
  // 下：40rpx
  // 左：0 (因为右边是0，根据CSS规则，左边的值会复制右边的值)
  padding: 10rpx 0 15rpx;
  flex-direction: column;

  .section-title {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;
    font-size: 36rpx;
    font-weight: bold;
    color: #66b3f6;
    margin-bottom: 12rpx;

    &::before,
    &::after {
      content: '';
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
  .section-subtitle {
    display: flex;
    justify-content: center;
    font-size: 24rpx;
    color: #99e5ff;
  }
}

// 加载提示文字
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}

// 公告栏
.notice-bar {
  display: flex;
  align-items: center;
  padding: 20rpx 30rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  margin: 20rpx;
  border-radius: $brand-border-radius;

  .notice-icon {
    font-size: 32rpx;
    margin-right: 16rpx;
  }

  .notice-text {
    flex: 1;
    font-size: 28rpx;
    color: #ffffff;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

// 推荐区域
.recommend-section {
  padding: 0 20rpx 40rpx;

  .section-header {
    display: flex;
    flex-direction: column;
    padding: 30rpx 10rpx 20rpx;

    .section-title {
      font-size: 36rpx;
      font-weight: bold;
      color: #333;
      margin-bottom: 8rpx;
    }

    .section-subtitle {
      font-size: 24rpx;
      color: #999;
    }
  }
}

// 玩家卡片列表
.player-list {
  display: flex;
  flex-direction: column;
  gap: 24rpx;
}

.player-card {
  display: flex;
  background: #ffffff;
  border-radius: 16rpx;
  padding: 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  transition: all 0.3s;

  &:active {
    transform: scale(0.98);
  }

  .player-avatar {
    width: 160rpx;
    height: 160rpx;
    border-radius: 12rpx;
    flex-shrink: 0;
    margin-right: 24rpx;
  }

  .player-info {
    flex: 1;
    display: flex;
    flex-direction: column;

    .player-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12rpx;

      .player-nickname {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
      }

      .player-rating {
        display: flex;
        align-items: center;
        background: #f2f7ff;
        padding: 4rpx 12rpx;
        border-radius: 20rpx;

        .rating-star {
          font-size: 24rpx;
          margin-right: 4rpx;
          color: #59b6f7 !important;
        }

        .rating-score {
          font-size: 24rpx;
          color: #59b6f7;
          font-weight: bold;
        }
      }
    }

    .player-intro {
      font-size: 26rpx;
      color: #666;
      line-height: 1.6;
      margin-bottom: 16rpx;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
    }

    .player-stats {
      display: flex;
      gap: 24rpx;
      margin-bottom: 16rpx;

      .stat-item {
        display: flex;
        align-items: center;
        gap: 8rpx;

        .stat-label {
          font-size: 24rpx;
          color: #999;
        }

        .stat-value {
          font-size: 24rpx;
          color: #333;
          font-weight: 500;
        }
      }
    }

    .player-tags {
      display: flex;
      flex-wrap: wrap;
      gap: 12rpx;
      margin-bottom: 20rpx;

      .tag-item {
        padding: 8rpx 16rpx;
        // background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        background: linear-gradient(135deg, #ecf6fe 0%, #ecf6fe 100%);
        color: #43a4f4;
        font-size: 22rpx;
        border-radius: 20rpx;
      }
    }

    .player-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .price-wrap {
        display: flex;
        align-items: baseline;

        .price-symbol {
          font-size: 24rpx;
          color: #ff4d4f;
          font-weight: bold;
        }

        .price-value {
          font-size: 40rpx;
          color: #ff4d4f;
          font-weight: bold;
          margin: 0 4rpx;
        }

        .price-unit {
          font-size: 22rpx;
          color: #999;
        }
      }

      // .order-btn {
      //   padding: 16rpx 40rpx;
      //   background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      //   color: #ffffff;
      //   font-size: 28rpx;
      //   border-radius: 40rpx;
      //   border: none;
      //   font-weight: 500;

      //   &::after {
      //     border: none;
      //   }
      // }
      // .order-btn {
      //   padding: 16rpx 40rpx;
      //   background: linear-gradient(135deg, #59b6f7 0%, #2d87f0 100%);
      //   color: #ffffff;
      //   font-size: 28rpx;
      //   border-radius: 40rpx;
      //   border: none;
      //   font-weight: 500;

      //   // 清除默认边框和背景
      //   border: none;
      //   background-color: transparent;

      //   // 清除伪元素边框
      //   &::after {
      //     border: none;
      //     display: none; // 或者直接隐藏
      //   }

      //   // 确保渐变背景正常显示
      //   background: linear-gradient(135deg, #59b6f7 0%, #2d87f0 100%) !important;
      // }
      .order-btn {
        padding: 16rpx 40rpx;
        // background: linear-gradient(135deg, #59b6f7 0%, #2d87f0 100%);
        // background: linear-gradient(135deg, #67b6f8 0%, #2d94f1 50%, #67b6f8 100%);
        background: linear-gradient(135deg, rgba(67, 164, 244, 0.9) 0%, rgba(67, 164, 244, 1) 100%);
        color: #ffffff;
        font-size: 28rpx;
        border-radius: 40rpx;
        font-weight: 500;
        text-align: center;

        // 点击效果
        &:active {
          opacity: 0.8;
          transform: scale(0.98);
        }
      }
    }
  }
}
</style>
